/*  Copyright (c) 2012 William Rogers and James Boud

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.ComponentModel;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;

using TInput = Microsoft.Xna.Framework.Content.Pipeline.Graphics.TextureContent;
using TOutput = JBBRXG11ContentTextureCube.TextureCubeInfoStuff;

namespace JBBRXG11ContentTextureCube
{
    [ContentProcessor(DisplayName = "JBBRXG11 TextureCube Processor")]
    public class JBBRXG11TextureCubeContentProcessor : ContentProcessor<TInput, TOutput>
    {
        #region Processor Parameters
        // For the processor attributes to work need "using System.ComponentModel"
        Color colourkey = new Color(255, 255, 255, 255);
        bool colourkeyenabled = false;
        bool generatemipmaps = false;
        bool resizetopoweroftwo = false;

        [DisplayName("Color Key Color")]
        [DefaultValue("255, 255, 255, 255")]
        public Color ColorKeyColor { get { return colourkey; } set { colourkey = value; } }

        [DisplayName("Colour Key Enabled")]
        [DefaultValue("False")]
        public bool ColorKeyEnabled { get { return colourkeyenabled; } set { colourkeyenabled = value; } }

        [DisplayName("Generate Mipmaps")]
        [DefaultValue("False")]
        public bool GenerateMipmaps { get { return generatemipmaps; } set { generatemipmaps = value; } }

        [DisplayName("Resize to Power of Two")]
        [DefaultValue("False")]
        public bool ResizeToPowerOfTwo { get { return resizetopoweroftwo; } set { resizetopoweroftwo = value; } }
        #endregion Processor Parameters

        public override TOutput Process(TInput input, ContentProcessorContext context)
        {
            TextureCubeInfoStuff tcis = new TextureCubeInfoStuff();
            TextureProcessor texproc = new TextureProcessor();
            texproc.ColorKeyColor = colourkey;
            texproc.ColorKeyEnabled = colourkeyenabled;
            texproc.GenerateMipmaps = generatemipmaps;
            texproc.ResizeToPowerOfTwo = resizetopoweroftwo;
            TextureContent output = texproc.Process(input, context);
            TextureCubeContent outcont = output as TextureCubeContent;

            if (outcont.Faces.Count != 6)
                throw new Exception("JBBRXG11: Texture cube should have six faces (only " + 
                                            outcont.Faces.Count.ToString() + " found");

            tcis.size = -1;
            tcis.faces = new JBBRXG11ContentTexture.Texture2DInfoStuff[6];
            for (int f = 0;  f < 6;  f++)
            {
                if (outcont.Faces[f][0].Width != outcont.Faces[f][0].Height)
                    throw new Exception("JBBRXG11:  Texture cube should have square faces");
                if (f == 0)
                {
                    tcis.size = outcont.Faces[f][0].Width;
                }
                else
                {
                    if (tcis.size != outcont.Faces[f][0].Width)
                        throw new Exception("JBBRXG11:  Texture cube faces should all be the same size");
                }
                MipmapChain chain = outcont.Faces[f];
                JBBRXG11ContentTexture.Texture2DInfoStuff tis = new JBBRXG11ContentTexture.Texture2DInfoStuff();
                
                tis.mipchain = new JBBRXG11ContentTexture.TextureBitmap[chain.Count];
                int m = 0;
                foreach (BitmapContent bm in chain)
                {
                    SurfaceFormat format;// = SurfaceFormat..Unknown;
                    bm.TryGetFormat(out format);
                    tis.mipchain[m].width = bm.Width;
                    tis.mipchain[m].height = bm.Height;
                    tis.mipchain[m].format = format;
                    byte[] bmpixdata = bm.GetPixelData();
                    tis.mipchain[m].data = bmpixdata;
                    m++;
                }

                tcis.faces[f] = tis;
            }

            return tcis;
        }
    }
}
